package v3.chapter2.synMoreObjectStaticOneLock;

public class Run {

	public static void main(String[] args) {
		Service service1 = new Service();
		ThreadA a = new ThreadA(service1);
		a.setName("a");
		a.start();
		
		Service service2 = new Service();
		ThreadB b = new ThreadB(service2);
		b.setName("b");
		b.start(); 
	}
}

class ThreadA extends Thread {
	private Service service;

	public ThreadA(Service service) {
		super();
		this.service = service;
	}

	public void run() {
		service.printA();
	}
}

class ThreadB extends Thread {
	private Service service;

	public ThreadB(Service service) {
		super();
		this.service = service;
	}

	public void run() {
		service.printB();
	}
} 

class Service {
	synchronized public static void printA() {
		try {
			System.out.println(
					"线程名称：" + Thread.currentThread().getName() + " 在 " + System.currentTimeMillis() + "进入 printA");
			Thread.sleep(3000);
			System.out.println(
					"线程名称：" + Thread.currentThread().getName() + " 在 " + System.currentTimeMillis() + "离开 printA");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	synchronized public static void printB() {
		System.out
				.println("线程名称：" + Thread.currentThread().getName() + " 在 " + System.currentTimeMillis() + "进入 printB");
		System.out
				.println("线程名称：" + Thread.currentThread().getName() + " 在 " + System.currentTimeMillis() + "离开 printB");
	}
}